<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/_fragments :: head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客发布</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-ui/2.2.10/semantic.min.css">
    <link rel="stylesheet" href="../../static/css/me.css" th:href="@{css/me.css}"> <!--引入自定义CSS样式-->
    <link rel="stylesheet" href="../../static/lib/editor/css/editormd.min.css" th:href="@{lib/editor/css/editormd.min.css}"><!--引入md编辑器css-->
</head>
<body>
    <!--导航-->
    <nav th:replace="admin/_fragments :: menu(1)" class="ui inverted attached segment me-padded-tb-mini me-shadow-small"> <!--引入css样式-->
        <div class="ui container">
            <div class="ui inverted secondary menu stackable"><!--stackable可堆叠，解决手机访问-->
                <h2 class="ui teal header item">后台管理</h2> <!--item为menu中的一个条目-->
                <a href="#" class="active m-item item me-mobile-hide"><i class="home icon"></i>博客</a><!--href为超链接-->
                <a href="#" class="m-item item me-mobile-hide"><i class="idea icon"></i>分类</a><!--m-item，在JQuery里获取样式-->
                <a href="#" class="m-item item me-mobile-hide"><i class="tags icon"></i>标签</a>

                <div class="right m-item me-mobile-hide menu">
                    <div class="ui dropdown item">
                        <div class="text">
                            <img src="../../static/images/wechat.jpg" alt="" class="ui avatar image">
                            泽宝宝
                        </div>
                        <i class="dropdown icon"></i>
                        <div class="menu">
                            <a href="#" class="item">注销</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <a href="#" class="ui black icon button me-top-right me-mobile-show menu toggle"> <!--手机端菜单图标, menu toggle获取点击事件-->            <i class="sidebar icon"></i>
        </a>
    </nav>

    <!--二级导航-->
    <div class="ui attached menu"> <!--active的就可以有小箭头-->
        <div class="ui container">
            <div class="right menu">
                <a href="#" th:href="@{/admin/blogs/input}" class="active blue item">发布</a>
                <a href="#" th:href="@{/admin/blogs}" class="teal item">列表</a>
            </div>
        </div>
    </div>

    <!--autosave后返回到空的-->
    <div th:fragment="autosave"></div>
    <!--中间内容-->
    <div class="me-container me-padded-tb-large">
        <div class="ui container">
            <form id="blog-form" action="#" th:object="${blog}" th:action="@{/admin/blogs}" method="post" class="ui form">

                <input type="hidden" name="autoSubmit">
                <input type="hidden" name="published">
                <input type="hidden" name="id" th:value="*{id}"> <!--如果再次编辑时id有值就拿到他-->
                <!--标题-->
                <div class="ui required field"><!--required: 不输入为红框-->
                    <div class="ui left labeled input">
                        <div class="ui teal basic label selection dropdown"><!--需要加label-->
                            <!--隐含域指定默认值-->
                            <input type="hidden" value="原创" name="flag" th:value="*{flag}!=null ? *{flag} : '原创'"><!--默认值-->
                            <i class="dropdown icon"></i>
                            <div class="text">原创</div>
                            <div class="ui teal menu">
                                <div class="item" data-value="原创">原创</div>
                                <div class="item" data-value="转载">转载</div>
                                <div class="item" data-value="翻译">翻译</div>
                            </div>
                        </div>
                        <input type="text" name="title" placeholder="标题" th:value="*{title}">
                    </div>
                </div>

                <!--编辑内容-->
                <div class="required field">
                    <div id="md-content" style="z-index: 1 !important;"> <!--最上方显示，全屏覆盖-->
                        <textarea style="display: none" name="content" placeholder="内容" cols="30" rows="10" th:text="*{content}"></textarea>
                    </div>
                </div>

                <!--分类和标签选择-->
                <div class="two fields"><!--field貌似是横着的？-->
                    <div class="required field">
                        <div class="ui left labeled action input"><!--action: 标签和下拉框变紧密-->
                            <label class="ui compact teal basic label">分类</label>
                            <div class="ui fluid tag search selection dropdown"><!--multiple, search 支持多选和搜索-->
                                <!--这里为hidden才能显示默认值，因为不是tag-->
                                <input type="hidden" name="typeId" th:value="*{type} != null ? *{type.id} : ''">
                                <i class="dropdown icon"></i>
                                <div class="default text" contenteditable="true">分类</div>
                                <div class="menu">
                                    <div th:each="type: ${types}" class="item" data-value="1" th:data-value="${type.id}" th:text="${type.name}">错误日志</div><!--data-value为传到后端的值-->
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="field">
                        <div class="ui left labeled action input"><!--action: 标签和下拉框变紧密-->
                            <label class="ui compact teal basic label">标签</label>
                            <div class="ui tag fluid multiple search selection dropdown">
                                <!--隐含域和data-value相等，会选中对应的标签-->
                                <input type="hidden" name="tagIds" th:value="*{tagIds}">
                                <i class="dropdown icon"></i>
                                <div class="default text">标签</div>
                                <div class="menu">
                                    <div th:each="tag: ${tags}" class="item" data-value="Java" th:data-value="${tag.id}" th:text="${tag.name}">Java</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!--首图引用地址+上传-->
                <div id="firstPicture-container">
                    <div th:fragment="firstPictureUpload">
                        <div class="ui grid">
                            <div class="twelve wide column">
                                <div class="required field">
                                    <div class="ui left action labeled input">
                                        <input type="hidden" name="blogId" >
                                        <label class="ui compact teal basic label">首图</label>
                                        <input type="text" name="firstPicture" th:value="*{firstPicture}==null ? ${path} : *{firstPicture}" placeholder="首图引用地址">
                                    </div>
                                </div>
                            </div>
                            <div class="two wide right aligned column">
                                <button type="button" id="upload-btn" class="ui teal button">上传</button>
                            </div>
                            <div class="two wide right aligned column">
                                <input type="file" id="file-input" style="display: none" name="fileContent" onchange="fileImport();">
                                <button type="button" id="local-btn" class="ui blue button">本地</button>
                            </div>
                        </div>
                        <!--<div id="upload-progress" class="ui small blue progress" style="visibility: hidden" data-value="50" data-total="100">
                            <div class="bar">
                                <div class="progress"></div>
                            </div>
                        </div>-->
<!--                        <progress class="ui small blue progress" style="visibility: hidden" id="upload-progress" value="0" max="100"></progress>-->
                        <div class="ui message" th:unless="${#strings.isEmpty(message)}">
                            <i class="close icon"></i>
                            <div class="header">提示：</div>
                            <p th:text="${message}">恭喜，操作成功！</p>
                        </div>
                    </div>
                </div>

                <p></p>

                <!--简短描述-->
                <div class="required field">
                    <div class="ui input">
                        <textarea name="description" th:text="*{description}" placeholder="请输入博客简介......" cols="30" rows="10"></textarea>
                    </div>
                </div>

                <!--推荐、可转载选择checkbox-->
                <div class="inline fields">
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="recommend" name="enableRecommend" th:checked="*{enableRecommend}" checked><!--hidden 默认不选中-->
                            <label for="recommend">推荐</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="shareInfo" name="enableShareStatement" th:checked="*{enableShareStatement}"><!--hidden 默认不选中-->
                            <label for="shareInfo">可转载</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="donate" name="enableDonate" th:checked="*{enableDonate}" class="hidden"><!--hidden 默认不选中-->
                            <label for="donate">可赞赏</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" id="comment" name="enableComment" th:checked="*{enableComment}" class="hidden"><!--hidden 默认不选中-->
                            <label for="comment">可留言</label>
                        </div>
                    </div>
                </div>

                <!--提交、返回按钮-->
                <div class="ui right aligned container">
                    <!--onclick内容为哪里来的就返回到哪里-->
                    <!--<button class="ui button" onclick="window.history.go(-1)">返回</button>&lt;!&ndash;默认事件是提交&ndash;&gt;-->
                    <button type="button" id="save-btn" class="ui secondary button">保存</button>
                    <button type="button" id="published-btn" class="ui teal button">发布</button>
                </div>
                <div class="ui error mini message"></div> <!--为空时跳出来的提示框-->

            </form>
        </div>
    </div>

    <!--底部footer-->
    <footer th:replace="admin/_fragments :: footer" class="ui inverted vertical segment me-padded-tb-max"><!--vertical消除圆角，用attached好像也行-->
        <div class="ui center aligned container"><!--居中-->
            <div class="ui inverted divided grid stackable"><!--grid stackable默认均匀分成16份, divided分割线-->
                <div class="three wide column">
                    <div class="ui inverted link list">
                        <div class="item">
                            <img src="../../static/images/wechat.jpg" class="ui rounded image" alt="" style="width: 110px"><!--alt规定图片无法显示时的替代文本-->
                        </div>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header me-text-space me-opacity-mini">最新博客</h4><!--标题-->
                    <div class="ui inverted link list">
                        <a href="#" class="item me-text-thin me-text-lined">用户故事</a>
                        <a href="#" class="item me-text-thin me-text-lined">用户故事</a>
                        <a href="#" class="item me-text-thin me-text-lined">用户故事</a>
                    </div>
                </div>
                <div class="three wide column">
                    <h4 class="ui inverted header me-text-space me-opacity-mini">联系我</h4><!--标题-->
                    <div class="ui inverted link list">
                        <a href="#" class="item me-text-thin me-text-lined">Email: stg1205@163.com</a>
                        <a href="#" class="item me-text-thin me-text-lined">QQ: 1014922487</a>
                    </div>
                </div>
                <div class="seven wide column">
                    <h4 class="ui inverted header me-text-space me-opacity-mini">介绍</h4>
                    <p class="me-text-space me-text-thin me-text-lined me-opacity-mini">这是我的个人博客，啦啦啦啦啦啦拉拉拉啊拉啦阿拉</p>
                </div>
            </div>
            <div class="ui inverted section divider"></div>
            <p class="me-text-space me-opacity-tiny">Copyright</p>
        </div>

    </footer>
    <th:block th:replace="_fragments :: script"></th:block>
    <script th:src="@{/lib/editor/editormd.js}" src="../../static/lib/editor/editormd.js"></script>
    <script th:src="@{/js/uploadImg.js}" type="text/javascript"></script>
    <script th:src="@{/js/firstPicture.js}" id="firstPicture-script"></script>
    <script th:src="@{/js/jquery.form.js}" type="text/javascript"></script>
    <script>
        // chrome中做了优化，用户做出改变才有效
        window.onbeforeunload = function(event) {
            //if (event.clientX > document.body.clientWidth && event.clientY < 0 || event.altKey) {
                event.returnValue = "确认退出？还没提交呢";
            //}
        }

        // 重新加载js
        function reloadFirstPictureJS(id, newJS) {
            var oldJS = document.getElementById(id);
            if (oldJS) oldJS.parentNode.removeChild(oldJS);
            var scriptObj = document.createElement("script");
            scriptObj.src = newJS;
            scriptObj.type = "text/javascript";
            scriptObj.id = id;
            document.getElementsByTagName("body")[0].appendChild(scriptObj);
        }

        $('.menu.toggle').click(function () {
            $('.m-item').toggleClass('me-mobile-hide'); //toggleClass，取反函数
        });

        /*下拉框*/
        $('.ui.dropdown').dropdown({
            on: 'hover', //鼠标放上就触发
        });

        /*标签的可输入新标签*/
        $('.ui.tag.dropdown').dropdown({
            allowAdditions: true
        });

        /*md编辑器初始化*/
        var testEditor;
        $(function() {
            testEditor = editormd("md-content", {
                width: "100%",
                height: 640,
                syncScrolling: "single",
                path : "/lib/editor/lib/", /*在thymeleaf模板下不用写static*/

                //saveHTMLToTextarea : true,    // 保存 HTML 到 Textarea
                //searchReplace : true,
                //watch : false,                // 关闭实时预览
                //htmlDecode : "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启
                //toolbar  : false,             //关闭工具栏
                //previewCodeHighlight : false, // 关闭预览 HTML 的代码块高亮，默认开启
                emoji : true,
                taskList : true,
                tocm : true,                   // Using [TOCM]
                tex : true,                   // 开启科学公式TeX语言支持，默认关闭
                flowChart : true,             // 开启流程图支持，默认关闭
                sequenceDiagram : true,       // 开启时序/序列图支持，默认关闭,
                //dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
                //dialogShowMask : false,     // 设置弹出层对话框显示透明遮罩层，全局通用，默认为true
                //dialogDraggable : false,    // 设置弹出层对话框不可拖动，全局通用，默认为true
                //dialogMaskOpacity : 0.4,    // 设置透明遮罩层的透明度，全局通用，默认值为0.1
                //dialogMaskBgColor : "#000", // 设置透明遮罩层的背景颜色，全局通用，默认为#fff

                toolbarIcons : function() {
                    // Or return editormd.toolbarModes[name]; // full, simple, mini
                    // Using "||" set icons align right.
                    return [
                        "link", "reference-link", "image", "|",
                        "table", "hr", "pagebreak", "|",
                        "code", "preformatted-text", "code-block", "|",
                        "emoji", "html-entities", "|",
                        "goto-line", "preview", "fullscreen", "|",
                        "help"
                    ]
                },

                //主题设置
                theme: "dark",
                previewTheme: "dark",
                editorTheme: 'pastel-on-dark',

                imageUpload : true,
                imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL : "/admin/blogs/upload/editorPicture",

                onload : function() {
                    initPasteDragImg(this);
                    //this.fullscreen();
                    //this.unwatch();
                    //this.watch().fullscreen();

                    //this.setMarkdown("#PHP");
                    //this.width("100%");
                    //this.height(480);
                    //this.resize("100%", 640);
                }
            });
        });

        $('#save-btn').click(function () {
            $('[name="autoSubmit"]').val(false);
            $('[name="published"]').val(false);
            $('#blog-form').submit();
        });

        $('#published-btn').click(function () {
            $('[name="autoSubmit"]').val(false);
            $('[name="published"]').val(true);
            $('#blog-form').submit();
        });

        //要加后面的""里的地址
        $('#newBlog-container').load(/*[[@{/footer/newBlogs}]]*/"/footer/newBlogs");
    </script>
</body>
</html>